/**
 * @classDescription  滑动构造函数
 * @type {Object}
 * @param {String | Object}	父层ID
 * @param {String}	活动的对象标签名
 * @param {String | Object}	控制左键
 * @param {String | Object}	控制右键
 * @param {Object}  参数集合（包括event, count, fadeIn, dir, loop, index, speed, size, callBack)
 */ 
var Slide = function(id, tag, ctrL, ctrR, option) {
	var el = Kg.$T(tag, id)[0];
	this.setOption(option);
	this.timer = null;
    this.tag = tag;
    this.id = id;
    this.father = Kg.$(id);
	this.crtL = Kg.$(ctrL);
	this.crtR = Kg.$(ctrR);
    this.size = this.option.dir === "Left"?this.father.offsetWidth:this.father.offsetHeight;
    this.items = [];
	this.itemSize = el[this.option.dir === "Left"?"offsetWidth":"offsetHeight"] + 
					parseInt(Kg.getStyle(el, "margin-" + this.option.dir.toLowerCase())) + 
					parseInt(Kg.getStyle(el, "margin-" + (this.option.dir === "Left"?"right":"bottom")));
	
	if(this.option.loop){
		var wrap = Kg.$T(this.tag, this.id)[0].parentNode;
		wrap.innerHTML += wrap.innerHTML;
	}

	this.init();
	this.l = this.option.loop?this.items.length/2:this.items.length;

	Kg.addEvent(this.crtL, this.option.event, Kg.bind(function(){
		this.prev();
	}, this));

	Kg.addEvent(this.crtR, this.option.event, Kg.bind(function(){
		this.next();
	}, this));

	this.currentIndex = this.option.index === "random"?
		parseInt(Math.random() * this.l):
		this.option.index;
	this.father["scroll"+this.option.dir] = this.itemSize * this.items[0].length * this.currentIndex;
	this.setCtrLeft();
	this.setCtrRight();
};

Slide.prototype.init = function(){
	var el = Kg.$T(this.tag, this.id);
	for (var i = 0; i < el.length; i++) {
        el[i].style.position = "absolute";
        el[i].style[this.option.dir.toLowerCase()] = this.itemSize * i + "px";
    }
    for (var i = 1, arr = []; i <= el.length; i++) {
        arr.push(el[i - 1]);
        if (i % this.option.count == 0) {
            this.items[this.items.length] = arr;
            arr = [];
        }
    } 
	! this.items.length && this.items.push(arr);
    el = null;
};

Slide.prototype.setCtrLeft = function(){
	if(this.currentIndex === 0){
		this.crtL.className += " disable_l";
	} else {
		this.crtL.className = this.crtL.className.replace(/\s*disable_l/, "");
	}
};

Slide.prototype.setCtrRight = function(){
	if(this.currentIndex === this.items.length - 1){
		this.crtR.className += " disable_r";
	} else {
		this.crtR.className = this.crtR.className.replace(/\s*disable_r/, "");
	}
};

Slide.prototype.setOption = function(option) {
    this.option = {
		event: "click",   //事件触发
		count:0,		//滚动元素的个数
		fadeIn: true,	//渐出效果
        dir: "Left",		//滚动方向
		loop: false,		//是否循环滚动
		index:	0,		//currentIndex
		speed: 0.1,		//滑动速度
		size:0,
		callBack:null
    };
    Kg.extend(this.option, option || {},true);
};

Slide.prototype.prev = function() {
    this.currentIndex === 0 ? this.option.loop?this.over(this.items.length - 1):null: this.over(this.currentIndex - 1);
};

Slide.prototype.next = function() {
    this.currentIndex === this.items.length - 1 ? this.option.loop?this.over(0):null: this.over(this.currentIndex + 1);
};

Slide.prototype.over = function(i) {
	clearTimeout(this.timer);
	if(this.option.loop){
		if(this.currentIndex == this.l*2 -1 && i == 0){
			this.father["scroll" + this.option.dir] = (this.l - 1)*this.size;
			i = this.l; 
		}
		if(this.currentIndex == 0 && i == this.l*2 - 1){
			this.father["scroll" + this.option.dir] = this.l*this.size;
			i = this.l - 1; 
		}
	}	
    this.currentIndex = i;
	var speed = Kg.UA.Ie?this.option.speed:this.option.speed-0.06;
    var start = this.father["scroll" + this.option.dir];
    var end = this.currentIndex * this.size;
	var _this = this;
	this.timer = Kg.slide(this.father, "scroll" + this.option.dir, start, end, speed, function(){
		clearInterval(_this.timer);
		_this.setCtrLeft();
		_this.setCtrRight();
	});
    this.option.fadeIn && this.fadeIn();
    this.option.callback && this.option.callback(this);
};

Slide.prototype.fadeIn = function(n) {
	var step = Kg.UA.Ie?3:1;
    for (var i = 0; i < this.items.length; i++) {
        if (i == this.currentIndex) {
            for (var j = 0; j < this.items[i].length; j++) {
                Kg.setOpacity(this.items[i][j], 0);
                Kg.fadein(this.items[i][j], 1, step);
            }
        }
    }
};

Slide.prototype.reset = function() {
    clearTimeout(this.timer);
    this.items = [];
    this.currentIndex = 0;
    this.father["scroll"+this.option.dir] = 0;
    this.init();
	this.setCtrLeft();
	this.setCtrRight();
};